import 'dart:ffi';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/emergency_event/view_model/emergency_event_record_view_model.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../bad_event/ui/bad_event_list_page.dart';
import '../../res/gaps.dart';
import '../../widgets/common_widget.dart';
class EmergencyEventRecordPage extends BaseStatefulMvvmWidget{
  @override
  State<StatefulWidget> createState() => _EmergencyEventRecordState();

}

class _EmergencyEventRecordState extends BaseMvvmListState<EmergencyEventRecordViewModel,EmergencyEventRecordPage>{

  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: key,
      backgroundColor: Colours.bg_color,
      appBar: AppBar(title: Text('紧急事件'),actions: [
        filterWidget().onTap(() {
          key.currentState?.openEndDrawer();
        })
      ],),
      body: Column(
          children: [
            loadingContentWidget.setWeight(1),
            TextButton(onPressed: (){
              viewModel.addRecord();
            }, child: CommonText('新增',textColor: Colors.white),style: badEventButtonStyle,).addToContainer(
              margin: EdgeInsets.only(left: 16,right: 16,top: 12,bottom: 20)
            )
          ],
      ),
      endDrawer: endDraw(),
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 12),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 16,
              ),
              ...[
                filterContent(
                    title: '事件定性', value: viewModel.eventNature?.name ?? "")
                    .onTap(() {
                  viewModel.chooseEventNature();
                }),

                filterContent(
                    title: '事件类型', value: viewModel.eventType?.name ?? "")
                    .onTap(() {
                  viewModel.chooseEventType();
                }),

                filterContent(
                    title: '发生时间', value: viewModel.startDateStr)
                    .onTap(() {
                  viewModel.selectStartDate(click: (date){
                    viewModel.requestRefresh();
                  });
                }),
              ].insertWidget(Gaps.dividerLine),
              Gaps.dividerLine,
            ],
          ),
        ).setWeight(1),
        SizedBox(
          height: 10,
        ),
        Row(
          children: [
            SizedBox(
              width: 16,
            ),
            TextButton(
              onPressed: () {
                key.currentState?.closeEndDrawer();
                viewModel.resetClick();
              },
              child: CommonText('重置',
                  textColor: Color(0xff333333), fontWeight: FontWeight.w500),
              style: ButtonStyle(
                  shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: 6.borderRadius,
                      side: BorderSide(color: Colours.gray_99)))),
            ).setWeight(1),
            SizedBox(
              width: 12,
            ),
            TextButton(
                onPressed: () {
                  key.currentState?.closeEndDrawer();
                  viewModel.requestRefresh();
                },
                child: CommonText('确定', textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.blue),
                    shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: 6.borderRadius,
                      side: BorderSide(color: Colors.blue),
                    )))).setWeight(1),
            SizedBox(
              width: 16,
            ),
          ],
        ),
        SizedBox(
          height: 12,
        )
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.85,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            CommonText(item.name,fontWeight: FontWeight.bold,textSize: 17),
              SizedBox(width: 10,),
              CommonText(item.roomNo),
              SizedBox().setWeight(1),
            CommonText(item.eventNatureName,textColor: Colors.red,fontWeight: FontWeight.w500)
          ],
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            CommonText('事件类型：${item.eventTypeName}'),
            CommonText('相关人类型：${item.relatedTypeName}'),

          ],
        ),

        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            CommonText('发生时间：${(item.startDate??"").changeTime(format: ['yyyy','-','mm','-','dd'])}'),

            CommonText('状态：${item.statusName}'),

          ],
        ),
      ].insertWidget(SizedBox(height: 10,)),
    ).onTap((){
      viewModel.toDetail(item);
    }).addToContainer(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: 6.borderRadius
      ),
      padding: EdgeInsets.symmetric(horizontal: 10,vertical: 12)
    );
  }

  @override
  Widget get separatorDivider => SizedBox(height: 12,);

  @override
  EdgeInsets get listPadding => EdgeInsets.symmetric(horizontal: 16,vertical: 12);

  @override
  EmergencyEventRecordViewModel createViewModel() {
    return EmergencyEventRecordViewModel(context);
  }

  @override
  bool get canPullUp => true;

}